﻿using SchemaExplorer;
using System;
using System.Collections.Generic;
using System.Text;

namespace BaoMen.CodeSmith.Framework
{
    /// <summary>
    /// 模块信息
    /// </summary>
    public abstract class Module
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbSchema"></param>
        public Module(DbSchema dbSchema)
        {
            DbSchema = dbSchema;
        }

        /// <summary>
        /// 数据库信息
        /// </summary>
        public DbSchema DbSchema { get; private set; }

        /// <summary>
        /// 命名空间
        /// </summary>
        public string Namespace { get; set; }

        /// <summary>
        /// 作者
        /// </summary>
        public string Author { get; set; }

        /// <summary>
        /// 生成时间
        /// </summary>
        public DateTime CreateTime => DateTime.Now;

        /// <summary>
        /// 获取类的名称（Pascal）
        /// </summary>
        /// <returns></returns>
        public virtual string GetClassName()
        {
            string className = DbSchema.TableSchema.Name;
            className = Util.RemovePrefix(className, DbSchema.TableNamePrefix);
            className = Util.RemoveSeparator(className, DbSchema.TableNameSeparator);
            return className;
        }

        /// <summary>
        /// 获取类的全名（Pascal）
        /// </summary>
        /// <returns></returns>
        public string GetClassFullName()
        {
            return string.Format("{0}.{1}", Namespace, GetClassName());
        }

        /// <summary>
        /// 获取属性的名称
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        public string GetPropertyName(ColumnSchema column)
        {
            return column.Name;
        }

        /// <summary>
        /// 是否需要复合主键
        /// </summary>
        /// <returns></returns>
        public bool IsComplexKey()
        {
            return DbSchema.TableSchema.PrimaryKey.MemberColumns.Count > 1;
        }

        /// <summary>
        /// 取得复合主键的类型
        /// </summary>
        /// <returns></returns>
        public string GetComplexKeyType()
        {
            string type = "Tuple<";
            foreach (ColumnSchema column in DbSchema.TableSchema.Columns)
            {
                if (column.IsPrimaryKeyMember)
                    type += GetCSharpTypeName(column) + ",";
            }
            type = type.Substring(0, type.Length - 1);
            type += ">";
            return type;
        }

        /// <summary>
        /// 获取c#类型名称
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        public string GetCSharpTypeName(ColumnSchema column)
        {
            return column.AllowDBNull ? Util.GetCSharpNullableTypeName(column) : Util.GetCSharpNotNullTypeName(column);
        }

        /// <summary>
        /// 获取c#类型名称(非空类型)
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        public string GetCSharpNotNullTypeName(ColumnSchema column)
        {
            return Util.GetCSharpNotNullTypeName(column);
        }

        /// <summary>
        /// 取得标识字段。若有主键返回主键字段，若没有返回第一个外键字段，若没有返回第一个字段
        /// </summary>
        /// <param name="table">表架构</param>
        /// <returns></returns>
        public ColumnSchema GetIdentityColumn()
        {
            if (DbSchema.TableSchema.HasPrimaryKey)
                return DbSchema.TableSchema.PrimaryKey.MemberColumns[0];
            else if (DbSchema.TableSchema.ForeignKeys.Count > 0)
                return DbSchema.TableSchema.ForeignKeyColumns[0];
            else
                return DbSchema.TableSchema.Columns[0];
        }
    }
}
